local hx__qianxi = fk.CreateSkill {
  name = "hx__qianxi",
}


hx__qianxi:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(hx__qianxi.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {skill_name = "hx__qianxi"}) then
      local choices = {"hx__qianxi0","hx__qianxi1","hx__qianxi2", "hx__qianxi3"}
      local choice = player.room:askToChoice(player, {
        choices = choices,
        skill_name = hx__qianxi.name,
        prompt = "#hx__qianxi-choice"
      })
      if choice then
        event:setCostData(self, choice)
        return true
      end
      return false
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player.dead then
      room:addPlayerMark(player, "@@hx__qianxi_mod-turn")
    end
    if event:getCostData(self) == "hx__qianxi0" then
      room:addPlayerMark(player, "@hx__qianxi_chusha", 2)
    elseif event:getCostData(self) == "hx__qianxi1" then
      player:drawCards(1, hx__qianxi.name)
      room:addPlayerMark(player, "@hx__qianxi_chusha", 1)
    elseif event:getCostData(self) == "hx__qianxi2" then
      player:drawCards(2, hx__qianxi.name)
    elseif event:getCostData(self) == "hx__qianxi3" then
      player:drawCards(3, hx__qianxi.name)
      room:addPlayerMark(player, "@hx__qianxi_jingsha", 1)
    end
    local cardDiscarded = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = hx__qianxi.name,
      cancelable = false,
      pattern = ".",
      prompt = "#qianxi-discard"
    })
    if player.dead then return end
    if not cardDiscarded or #cardDiscarded == 0 then return end
    local color = Fk:getCardById(cardDiscarded[1]):getColorString()
    local targets = table.filter(room:getOtherPlayers(player, false), Util.IdMapper)
    if #targets == 0 then return end
    local chosen = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#hx__qianxi-choose:::" .. color,
      skill_name = hx__qianxi.name,
      cancelable = false
    })
    local targetId = chosen[1]
    if not targetId then return end
    room:setPlayerMark(targetId, "@qianxi-turn", color)
    room:setPlayerMark(player, "_hx__qianxi_target-turn", targetId.id)
  end,
})

hx__qianxi:addEffect("viewas", {
  anim_type = "offensive",
  prompt = "#hx__qianxi_mod",
  pattern = ".|.|.|.|.|basic",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(hx__qianxi.name, all_names)
    if #names > 0 then
      return UI.ComboBox { choices = names, all_choices = all_names }
    end
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected ==0 and Fk:getCardById(to_select).trueName == "slash"
  end,
  view_as = function(self, player,cards)
    if not self.interaction.data then return end
    local choice = self.interaction.data
    if  #cards ~= 1 then return end
    local card = Fk:cloneCard(choice)
    card:addSubcards(cards)
    card.skillName = hx__qianxi.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getMark("@@hx__qianxi_mod-turn") > 0
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("@@hx__qianxi_mod-turn") > 0
  end,
})

hx__qianxi:addEffect(fk.Damage, {
  mute = true,
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    if target ~= player then return end
    if event == fk.Damage then
      return not data.to.dead and player:getMark("_hx__qianxi_target-turn") == data.to.id
      and data.card and data.card.trueName == "slash" and player.phase ~= Player.NotActive
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "_hx__qianxi_done-turn", 1)
  end,
})
hx__qianxi:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    if target ~= player then return end
    return player:getMark("@qianxi-turn") ~= 0 or player:getMark("_hx__qianxi_done-turn") > 0
    or player:getMark("@hx__qianxi_chusha") > 0 or player:getMark("@hx__qianxi_jingsha") > 0
    or player:getMark("@@hx__qianxi_mod-turn") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@qianxi-turn", 0)
    room:setPlayerMark(player, "@@hx__qianxi_mod-turn", 0)
    room:setPlayerMark(player, "@hx__qianxi_chusha", 0)
    room:setPlayerMark(player, "@hx__qianxi_jingsha", 0)
    local p = room:getPlayerById(player:getMark("_hx__qianxi_target-turn"))
    local color = p:getMark("@qianxi-turn")
    room:setPlayerMark(p, "@qianxi-turn", color == "red" and "black" or "red")
  end,
})

hx__qianxi:addEffect("targetmod", {
  mute = true,
  is_delay_effect = true,
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill("hx__qianxi") and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return player:getMark("@hx__qianxi_chusha")
    end
    return 0
  end,
})

hx__qianxi:addEffect("prohibit", {
  mute = true,
  is_delay_effect = true,
  prohibit_use = function(self, player, card)
    if player:getMark("@hx__qianxi") ~= 0 and card:getColorString() == player:getMark("@hx__qianxi") then return true end
    if player:getMark("@qianxi-turn") ~= 0 and card:getColorString() == player:getMark("@qianxi-turn") then return true end
    if player:getMark("@hx__qianxi_jingsha") > 0 and card.trueName == "slash"then return true end
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@hx__qianxi") ~= 0 and card:getColorString() == player:getMark("@hx__qianxi") then return true end
    if player:getMark("@qianxi-turn") ~= 0 and card:getColorString() == player:getMark("@qianxi-turn") then return true end
  end,
})

hx__qianxi:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target and target == player and player:hasSkill(hx__qianxi.name) and data.to ~= player and not data.to.dead and
      data.card and data.card.trueName == "slash"
  end,
  on_cost = function(self, event, target, player, data)
    local invoked = player.room:askToSkillInvoke(player, {
      skill_name = hx__qianxi.name,
      prompt = "#hx__qianxi-invoke::"..data.to.id
    })
    if invoked then
      event:setCostData(self, data.to.id)
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data:preventDamage()
    room:changeMaxHp(data.to, -1)
  end,
})

return hx__qianxi